package com.zhangchuchu.neohealth.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhangchuchu.neohealth.entity.Article;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticleMapper extends BaseMapper<Article> {

    @Select("SELECT title,content,author,published_at,image_url,likes FROM articles ORDER BY likes DESC LIMIT 4")
    List<Article> getTop4ArticlesByLikes();

    @Select("SELECT COUNT(*) FROM article_likes WHERE user_id = #{userId} AND article_id = #{articleId}")
    int checkUserLiked(@Param("userId") Integer userId, @Param("articleId") Integer articleId);

    @Insert("INSERT INTO article_likes (user_id, article_id) VALUES (#{userId}, #{articleId})")
    void insertLike(@Param("userId") Integer userId, @Param("articleId") Integer articleId);

    @Update("UPDATE articles SET likes = likes + 1 WHERE id = #{articleId}")
    void incrementLikes(@Param("articleId") Integer articleId);


}
